package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.exception.CustomException;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
@Transactional
public class WmMaterialServiceImpl implements WmMaterialService {

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private FileStorageService fileStorageService;
    /**
     * 完成文件上传
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadFile(MultipartFile multipartFile) throws IOException {

        WmUser wmUser = WmThreadLocalUtil.getUser();
        if (null == wmUser) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }

        String fileName = UUID.randomUUID().toString().replaceAll("-","");
        String sub = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));


        String url = fileStorageService.uploadImgFile("", fileName + sub, multipartFile.getInputStream());

        WmMaterial entity = new WmMaterial();
        entity.setCreatedTime(new Date());
        entity.setIsCollection((short)0);
        entity.setType((short)0);

        entity.setUserId(wmUser.getId());
        entity.setUrl(url);
        wmMaterialMapper.insert(entity);
        return ResponseResult.okResult(entity);
    }

    /**
     * 素材列表查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //1.检查传入参数是否合法
        dto.checkParam();
        //2.构建分页
        IPage pageInfo = new Page<>(dto.getPage(),dto.getSize());
        //3.查询 条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getIsCollection() != null , WmMaterial::getIsCollection , dto.getIsCollection());

        WmUser wmUser = WmThreadLocalUtil.getUser();
        if (null == wmUser) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        queryWrapper.eq(WmMaterial::getUserId, wmUser.getId());
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        //4.查询
        wmMaterialMapper.selectPage(pageInfo,queryWrapper);

        //5.封装
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)pageInfo.getTotal());
        pageResponseResult.setData(pageInfo.getRecords());




        return pageResponseResult;
    }
}
